﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{
    public partial class TiepNhanKhachMoi : Form
    {
        KhachHangDTO khachHangMoi = null;

        public TiepNhanKhachMoi()
        {
            InitializeComponent();
        }

        private void TiepNhanKhachMoi_Load(object sender, EventArgs e)
        {
            layDSLoaiKhach();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void layDSLoaiKhach()
        {
            List<LoaiKhachDTO> dsLoaiKhach = LoaiKhachBUS.layDSLoaiKhach();
            cbb_loaiKhach.DataSource = dsLoaiKhach;
            cbb_loaiKhach.DisplayMember = "TenLoaiKhach";
        }

        private void cbb_loaiKhach_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

       public KhachHangDTO layKhachThemVao()
        {
            return khachHangMoi;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            KhachHangDTO khachThemVao = new KhachHangDTO();
            LoaiKhachDTO loaiKhachThemVao = (LoaiKhachDTO)cbb_loaiKhach.Items[cbb_loaiKhach.SelectedIndex];

            if ((tb_TenKhach.Text.Trim() != null) && (cbb_loaiKhach.SelectedIndex >= 0))
            {
                try
                {
                    

                    khachThemVao.TenKhachHang = tb_TenKhach.Text.Trim();
                    khachThemVao.CMND = tb_CMND.Text.Trim();
                    khachThemVao.DiaChi = tb_DiaChi.Text.Trim();
                    khachThemVao.LoaiKhach = loaiKhachThemVao;
                    long maLoaiKhach = loaiKhachThemVao.MaLoaiKhach;

                    bool ketQua = KhachHangBUS.them(khachThemVao, maLoaiKhach);
                    if (ketQua == false)
                        throw new Exception();

                    MessageBox.Show("Thêm khách thành công");
                    khachHangMoi = khachThemVao;
                    khachThemVao.MaKhachHang = KhachHangBUS.layMaKhachLonNhat();
                    this.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Thêm khách thất bại");
                }
            }

        }
    }
}
